package com.miaoxun.service.impl;

import com.basemapper.BasicMapper;
import com.miaoxun.common.util.Underline2Camel;
import com.miaoxun.dao.util.ClassUtil;
import com.miaoxun.service.BaseService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


public abstract class BaseServiceImpl<T> implements BaseService<T> {
    protected abstract BasicMapper<T> getMapper();

    @Transactional(rollbackFor = Exception.class)
    @Override
    public T save(T entity) {
        int insert = getMapper().insert(entity);
        if (insert > 0)
            return entity;
        return null;

    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Object key) {
        return getMapper().deleteById(key, currentClazzName()) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delete(T entity) {
        entity = null;
        return getMapper().delete(entity) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean update(T entity) {


        return getMapper().update(entity) > 0;
    }


    @Override
    public List<T> getAll() {
        return getMapper().getAll(currentClazzName());
    }

    @Override
    public T getModelById(Object key) {
        return getMapper().getModelById(key, currentClazzName());
    }

    @Override
    public List<T> getModelList(Map<String, String> map) {
        return getMapper().getModelList(map, currentClazzName());
    }


    @Override
    public int batchRemove(Object[] Ids) {
        return getMapper().batchRemove(Ids, currentClazzName());
    }

    private String currentClazzName() {
      return   (getClass().getSimpleName().replace("ServiceImpl", ""));

    }



























}
